package org.swim.servlet;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityNotFoundException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.swim.entity.User;
import org.swim.session.LoginManagerRemote;

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
		request.getRequestDispatcher("WEB-INF/Login.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher disp = null;
		boolean blockLogin = false;
		HttpSession session = request.getSession();
		String email = (String) request.getParameter("inputEmail");
		String inputPassword = (String) request.getParameter("inputPassword");
		session.setAttribute("email", email);
		request.setAttribute("password", inputPassword);
    	if (email.equals("")) {
    		request.setAttribute("emailStatus", "noEmailInserted");
			blockLogin = true;
		} else if (!(email.contains("@") || email.contains("."))) {
			request.setAttribute("emailStatus", "wrongEmailInserted");
			blockLogin = true;
		} else {
			request.setAttribute("emailStatus", "ok");
		}	
		if (inputPassword.equals("")) {
			request.setAttribute("passwordStatus", "noPasswordInserted");
			blockLogin = true;
		} else {
			request.setAttribute("passwordStatus", "ok");
		}		
    	if (blockLogin == false){
    		InitialContext ic;
    		try {
				ic = new InitialContext();
				LoginManagerRemote loginManager = (LoginManagerRemote)ic.lookup("LoginManagerRemote");
				User utente = loginManager.verifyLogin(email, inputPassword);
				if (utente!=null) {
					session.setAttribute("user", utente);
					request.setAttribute("login", "ok");
					if (utente.isAdmin()==true) {
				      	session.setAttribute("status", "admin");
					} else {
				      	session.setAttribute("status", "user");			
					}
			      	session.setAttribute("id", utente.getId());			
				   	disp = request.getRequestDispatcher("Profile?id=" + utente.getId());
				} else {
					request.setAttribute("login", "failed");
					request.setAttribute("passwordStatus", "wrongPassword");
					disp = request.getRequestDispatcher("WEB-INF/Login.jsp");
				}
			} catch (NamingException e) {
				e.printStackTrace();
			} catch (EntityNotFoundException e) {
				request.setAttribute("emailStatus", "notFound");
				request.setAttribute("login", "failed");
				disp = request.getRequestDispatcher("WEB-INF/Login.jsp");				
			}
		} else {
			request.setAttribute("status", "guest");
			disp = request.getRequestDispatcher("WEB-INF/Login.jsp");
		}
     	disp.forward(request,response);		
	}

}
